#!/usr/bin/env bash
#
# Summary: Set or show the shell-specific Node version
#
# Usage: nodenv shell <version>
#        nodenv shell --unset
#
# Sets a shell-specific Node version by setting the `NODENV_VERSION'
# environment variable in your shell. This version overrides local
# application-specific versions and the global version.
#
# <version> should be a string matching a Node version known to nodenv.
# The special version string `system' will use your default system Node.
# Run `nodenv versions' for a list of available Node versions.

set -e
[ -n "$NODENV_DEBUG" ] && set -x

# Provide nodenv completions
if [ "$1" = "--complete" ]; then
  echo --unset
  echo system
  exec nodenv-versions --bare
fi

version="$1"
shell="$(basename "${NODENV_SHELL:-$SHELL}")"

if [ -z "$version" ]; then
  if [ -z "$NODENV_VERSION" ]; then
    echo "nodenv: no shell-specific version configured" >&2
    exit 1
  else
    echo "echo \"\$NODENV_VERSION\""
    exit
  fi
fi

if [ "$version" = "--unset" ]; then
  case "$shell" in
  fish )
    echo "set -e NODENV_VERSION"
    ;;
  * )
    echo "unset NODENV_VERSION"
    ;;
  esac
  exit
fi

# Make sure the specified version is installed.
if nodenv-prefix "$version" >/dev/null; then
  case "$shell" in
  fish )
    echo "setenv NODENV_VERSION \"${version}\""
    ;;
  * )
    echo "export NODENV_VERSION=\"${version}\""
    ;;
  esac
else
  echo "false"
  exit 1
fi
